package com.hqyj.javaGraduation.hotel.account.service.Impl;

import com.hqyj.javaGraduation.hotel.account.dao.AccountUserMapper;
import com.hqyj.javaGraduation.hotel.account.dao.AccountUserRoleMapper;
import com.hqyj.javaGraduation.hotel.account.pojo.AccountUser;
import com.hqyj.javaGraduation.hotel.account.pojo.AccountUserRole;
import com.hqyj.javaGraduation.hotel.account.service.AccountUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

@Service
public class AccountUserServiceImpl implements AccountUserService {
    @Autowired(required = false)
    private AccountUserMapper accountUserMapper;
    @Autowired(required = false)
    private AccountUserRoleMapper accountUserRoleMapper;


    @Override
    public HashMap<String, Object> addAccountUser(AccountUser accountUser) {
        HashMap<String,Object> map = new HashMap<>();
        accountUser.setUserCreateDate(new Date());
        int member = Integer.parseInt(accountUser.getUserMember());
        if (member == 1 ){
            accountUser.setUserMember("普通会员");
        }
        else if (member == 2 ){
            accountUser.setUserMember("黄金会员");
        }
        else if (member == 3 ){
            accountUser.setUserMember("砖石会员");
        }
        int i = accountUserMapper.insert(accountUser);
        if (i>0){
            AccountUserRole accountUserRole = new AccountUserRole();
            accountUserRole.setUserId(accountUser.getUserId());
            accountUserRole.setRoleId(3);
            accountUserRoleMapper.insert(accountUserRole);
            map.put("code","ok");
            map.put("info","添加会员成功");
        }else {
            map.put("code","false");
            map.put("info","添加会员失败");
        }

        return map;
    }

    @Override
    public HashMap<String, Object> listAccountUser(AccountUser accountUser) {
        return null;
    }

    @Override
    public HashMap<String, Object> updateAccountUser(AccountUser accountUser) {
        HashMap<String,Object> map = new HashMap<>();
        int member = Integer.parseInt(accountUser.getUserMember());
        if (member == 1 ){
            accountUser.setUserMember("普通会员");
        }
        else if (member == 2 ){
            accountUser.setUserMember("黄金会员");
        }
        else if (member == 3 ){
            accountUser.setUserMember("砖石会员");
        }

        int i = accountUserMapper.updateById(accountUser);
        if (i>0){
            map.put("code","ok");
            map.put("info","修改成功");
        }else {
            map.put("code","false");
            map.put("info","修改失败");
        }

        return map;
    }

    @Override
    public HashMap<String, Object> delAccountUser(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        int i = accountUserMapper.deleteById(id);
        if (i>0){
            accountUserRoleMapper.deleteUserRoleByUserId(id);
            map.put("code","ok");
            map.put("info","删除成功");
        }else {
            map.put("code","false");
            map.put("info","删除失败");
        }

        return map;
    }
}
